package com.spic.business.plan.drill.service.impl;

import com.spic.business.plan.drill.domain.PlanDrillPlan;
import com.spic.business.plan.drill.domain.PlanDrillPlanDetail;
import com.spic.business.plan.drill.mapper.PlanDrillPlanMapper;
import com.spic.business.plan.drill.service.IPlanDrillPlanService;
import com.spic.common.core.utils.DateUtils;
import com.spic.common.core.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 应急预案演练计划Service业务层处理
 * 
 * @author spic
 * @date 2021-10-15
 */
@Service
public class PlanDrillPlanServiceImpl implements IPlanDrillPlanService 
{
    @Autowired
    private PlanDrillPlanMapper planDrillPlanMapper;

    /**
     * 查询应急预案演练计划
     * 
     * @param id 应急预案演练计划主键
     * @return 应急预案演练计划
     */
    @Override
    public PlanDrillPlan selectPlanDrillPlanById(Long id)
    {
        return planDrillPlanMapper.selectPlanDrillPlanById(id);
    }

    /**
     * 查询应急预案演练计划列表
     * 
     * @param planDrillPlan 应急预案演练计划
     * @return 应急预案演练计划
     */
    @Override
    public List<PlanDrillPlan> selectPlanDrillPlanList(PlanDrillPlan planDrillPlan)
    {
        return planDrillPlanMapper.selectPlanDrillPlanList(planDrillPlan);
    }

    /**
     * 新增应急预案演练计划
     * 
     * @param planDrillPlan 应急预案演练计划
     * @return 结果
     */
    @Transactional
    @Override
    public int insertPlanDrillPlan(PlanDrillPlan planDrillPlan)
    {
        planDrillPlan.setCreateTime(DateUtils.getNowDate());
        int rows = planDrillPlanMapper.insertPlanDrillPlan(planDrillPlan);
        insertPlanDrillPlanDetail(planDrillPlan);
        return rows;
    }

    /**
     * 修改应急预案演练计划
     * 
     * @param planDrillPlan 应急预案演练计划
     * @return 结果
     */
    @Transactional
    @Override
    public int updatePlanDrillPlan(PlanDrillPlan planDrillPlan)
    {
        planDrillPlan.setUpdateTime(DateUtils.getNowDate());
        planDrillPlanMapper.deletePlanDrillPlanDetailByDrillPlanId(planDrillPlan.getId());
        insertPlanDrillPlanDetail(planDrillPlan);
        return planDrillPlanMapper.updatePlanDrillPlan(planDrillPlan);
    }

    /**
     * 批量删除应急预案演练计划
     * 
     * @param ids 需要删除的应急预案演练计划主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deletePlanDrillPlanByIds(Long[] ids)
    {
        planDrillPlanMapper.deletePlanDrillPlanDetailByDrillPlanIds(ids);
        return planDrillPlanMapper.deletePlanDrillPlanByIds(ids);
    }

    /**
     * 删除应急预案演练计划信息
     * 
     * @param id 应急预案演练计划主键
     * @return 结果
     */
    @Override
    public int deletePlanDrillPlanById(Long id)
    {
        planDrillPlanMapper.deletePlanDrillPlanDetailByDrillPlanId(id);
        return planDrillPlanMapper.deletePlanDrillPlanById(id);
    }

    /**
     * 新增应急预案计划演练明细信息
     * 
     * @param planDrillPlan 应急预案演练计划对象
     */
    public void insertPlanDrillPlanDetail(PlanDrillPlan planDrillPlan)
    {
        List<PlanDrillPlanDetail> planDrillPlanDetailList = planDrillPlan.getPlanDrillPlanDetailList();
        Long id = planDrillPlan.getId();
        if (StringUtils.isNotNull(planDrillPlanDetailList))
        {
            List<PlanDrillPlanDetail> list = new ArrayList<PlanDrillPlanDetail>();
            for (PlanDrillPlanDetail planDrillPlanDetail : planDrillPlanDetailList)
            {
                planDrillPlanDetail.setDrillPlanId(id);
                list.add(planDrillPlanDetail);
            }
            if (list.size() > 0)
            {
                planDrillPlanMapper.batchPlanDrillPlanDetail(list);
            }
        }
    }

    /**
     * 查询应急预案演练计划详情
     *
     * @param id 查询应急预案演练计划详情主键
     * @return 应急预案演练计划详情
     */
    @Override
    public PlanDrillPlanDetail selectPlanDrillPlanDetailById (Long id){
        PlanDrillPlanDetail s = planDrillPlanMapper.selectPlanDrillPlanDetailById(id);
        return s;
    }
}
